package com.nl.lib_core.tool.extends

import android.app.Activity
import android.content.pm.ActivityInfo
import android.content.res.Configuration
import android.content.res.Resources
import android.graphics.Bitmap
import android.util.DisplayMetrics
import android.view.View
import android.view.WindowManager
import android.view.inputmethod.InputMethodManager

/** 作者: wangfei 创建时间: 2021/9/1 包名: com.nl.lib_core.util.extends 类名: ActivityExtends :屏幕截图 */
fun Activity.screenShot(activity: Activity, isDeleteStatusBar: Boolean = true): Bitmap {
  val decorView = activity.window.decorView
  decorView.isDrawingCacheEnabled = true
  decorView.buildDrawingCache()
  val bmp = decorView.drawingCache
  val dm = DisplayMetrics()
  activity.windowManager.defaultDisplay.getMetrics(dm)
  var ret =
      if (isDeleteStatusBar) {
        val resources = activity.resources
        val resourceId = resources.getIdentifier("status_bar_height", "dimen", "android")
        val statusBarHeight = resources.getDimensionPixelSize(resourceId)
        Bitmap.createBitmap(
            bmp, 0, statusBarHeight, dm.widthPixels, dm.heightPixels - statusBarHeight)
      } else {
        Bitmap.createBitmap(bmp, 0, 0, dm.widthPixels, dm.heightPixels)
      }
  decorView.destroyDrawingCache()
  return ret!!
}

/** 是否竖屏 */
fun Activity.isPortrait(): Boolean {
  return resources.configuration.orientation === Configuration.ORIENTATION_PORTRAIT
}

/** 是否横屏 */
fun Activity.isLandscape(): Boolean {
  return resources.configuration.orientation === Configuration.ORIENTATION_LANDSCAPE
}

/** 设置竖屏 */
fun Activity.setPortrait() {
  this.requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT
}

/** 设置横屏 */
fun Activity.setLandscape() {
  this.requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE
}

/** 设置全屏 */
fun Activity.setFullScreen() {
  this.window.addFlags(
      WindowManager.LayoutParams.FLAG_FULLSCREEN or
          WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS)
}

/** 显示软键盘 */
fun Activity.showKeyboard() {
  var imm: InputMethodManager =
      this.getSystemService(Activity.INPUT_METHOD_SERVICE) as InputMethodManager
  var view = this.currentFocus
  if (view == null) {
    view = View(this)
    view.isFocusable = true
    view.isFocusableInTouchMode = true
    view.requestFocus()
  }
  imm.showSoftInput(view, InputMethodManager.SHOW_FORCED)
}

/** 隐藏软键盘 */
fun Activity.hideKeyboard() {
  var imm: InputMethodManager =
      this.getSystemService(Activity.INPUT_METHOD_SERVICE) as InputMethodManager
  var view = this.currentFocus
  if (view == null) {
    view = View(this)
  }
  imm.hideSoftInputFromWindow(view.windowToken, 0)
}

fun Activity.getIdFromDrawable(resName: String): Int {
  val res: Resources = this.resources
  return res.getIdentifier(resName, "drawable", packageName)
}
